package com.hkbigdata.reallywar;

import com.hkbigdata.bean.UserBehavior;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @author liuanbo
 * @creat 2024-04-23-17:33
 * @see 2194550857@qq.com
 */
public class Flink01_Project_PV_Process {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment emv = StreamExecutionEnvironment.getExecutionEnvironment();

        emv.setParallelism(1);

        SingleOutputStreamOperator<UserBehavior> map = emv.readTextFile("input/UserBehavior.csv")
                .map((String line) -> {
                    String[] split = line.split(",");
                    return new UserBehavior(
                            Long.valueOf(split[0]),
                            Long.valueOf(split[1]),
                            Integer.valueOf(split[2]),
                            split[3],
                            System.currentTimeMillis()
                    );
                });

        map.filter(data -> "pv".equals(data.getBehavior()))
                .keyBy(UserBehavior::getBehavior)
                .process(new KeyedProcessFunction<String, UserBehavior, Integer>() {
                    private Integer count = 0;

                    @Override
                    public void processElement(UserBehavior value, Context ctx, Collector<Integer> out) throws Exception {
                        count++;
                        out.collect(count);
                    }
                })
                .print();


        emv.execute();
    }
}
